import { forwardRef, useContext, useEffect, useRef, useState } from "react";
import { NonStatSampleParameter, SampleParameterValues, SampleParmeterProps, SampleStepInfo, StatSampleParameter } from "../../typing";
import { SampleStepInfoContext } from "../samplingRecordPopup";
import { HocAntTabsFillStyleTab } from "~/public/cpas-ui/components-ui/ant-components/AntTab";
import AntTabsPanel from "~/public/cpas-ui/components-ui/ant-components/AntTab/AntTabsPanel";
import { StatSample } from "./StatSample";
import { NotStatSample } from "./NonStatSample";
import { consts } from "../../consts";
/**
 * 抽样参数
 */
const SampleParmeter = forwardRef((props: SampleParmeterProps, ref: any) => {
    //   const sampleStepInfo: SampleStepInfo = useContext(SampleStepInfoContext);

    const notStatSampleRef = useRef<any>();//非统计抽样
    const statSampleRef = useRef<any>(null);//统计抽样
    //各个tab标题表格加载
    const tabsTitle = consts.sampleMethodTabsTitle;
    const [tabActiveKey, setTabActiveKey] = useState<string>(props.sampleStepInfoOfParams?.sampleMethodActiveKey || 'tabNonStatSample');

    //每次渲染都要执行,因为notStatSampleRef和statSampleRef的变化,不会渲染
    useEffect(() => {
        if (ref) {
            ref.current = {
                sampleMethodActiveKey: tabActiveKey,
                getSampleParameterValues: () => {
                    return tabActiveKey == 'tabNonStatSample' ?
                        notStatSampleRef.current?.getSampleParameterValues()
                        : statSampleRef.current?.getSampleParameterValues()
                },
                setSampleParameterValues: (value: SampleParameterValues) => {
                    return tabActiveKey == 'tabNonStatSample' ?
                        notStatSampleRef.current?.setSampleParameterValues(value)
                        : statSampleRef.current?.setSampleParameterValues(value)
                },
                calcAmountSampleSizeAndUpdate: async () => {//计算重大金额和极不重要金额
                    return tabActiveKey == 'tabNonStatSample' ?
                        await notStatSampleRef.current?.calcAmountSampleSizeAndUpdate()
                        : await statSampleRef.current?.calcAmountSampleSizeAndUpdate()
                }
            }
        }
    }, [tabActiveKey])

    return <>
        <div style={{ /* border: '1px solid green',  */height: '80vh' }}>
            <HocAntTabsFillStyleTab key="sampleParameter"
                activeKey={tabActiveKey}
                onChange={(newTabActiveKey: string) => {
                    setTabActiveKey(newTabActiveKey)
                }
                }  >
                {/* 非统计抽样 */}
                <AntTabsPanel key='tabNonStatSample' tab={tabsTitle.tabNonStatSample}>
                    {tabActiveKey == 'tabNonStatSample' && <NotStatSample
                        ref={notStatSampleRef}
                        {...props}
                    />}
                </AntTabsPanel>
                {/* 统计抽样 */}
                <AntTabsPanel key='tabStatSample' tab={tabsTitle.tabStatSample}>
                    {tabActiveKey == 'tabStatSample' && <StatSample
                        ref={statSampleRef}
                        {...props}
                    />}
                </AntTabsPanel>
            </HocAntTabsFillStyleTab>
        </div>
    </>
});
export default SampleParmeter;